package com.wwj.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.springboot.entity.CmfzGuru;
import com.wwj.springboot.mapper.CmfzGuruMapper;
import com.wwj.springboot.service.CmfzGuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wwj
 */
@Service
@Transactional
public class CmfzGuruServiceImpl extends ServiceImpl<CmfzGuruMapper, CmfzGuru> implements CmfzGuruService {

    @Autowired
    private CmfzGuruMapper cmfzGuruMapper;

    @Override
    @Transactional(readOnly = true)
    public Map selectByPage(int page, int limit,String  username) {
        int count = cmfzGuruMapper.selectCounts(username);
        int offset=(page-1)*limit;
        List<CmfzGuru> list = cmfzGuruMapper.selectByPage(offset,limit,username);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;

    }

    @Override
    public Map addGuru(CmfzGuru guru) {
        Map map = new HashMap();
        try {
            cmfzGuruMapper.insert(guru);
            map.put("insert",true);
        } catch (Exception exception) {
            map.put("insert",false);
            exception.printStackTrace();
        }
        return map;
    }
    @Override
    public Map updateGuru(CmfzGuru guru) {
        Map map = new HashMap();
        try {
            cmfzGuruMapper.updateById(guru);
            map.put("update",true);
        } catch (Exception exception) {
            map.put("update",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    public Map deleteGuru(int id) {
        Map map = new HashMap();
        try {
            cmfzGuruMapper.deleteById(id);
            map.put("delete",true);
        } catch (Exception exception) {
            map.put("delete",false);
            exception.printStackTrace();
        }
        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public CmfzGuru selectOne(int id) {
        QueryWrapper<CmfzGuru> wrapper = new QueryWrapper<>();
        wrapper.eq("guru_id",id);
        return cmfzGuruMapper.selectOne(wrapper);
    }

    @Override
    public List selectAll() {
        return cmfzGuruMapper.selectList(null);
    }

    @Override
    public Map uploadList(List<CmfzGuru> cmfzGurus) {
        Map map = new HashMap();
        try {
            cmfzGuruMapper.uploadList(cmfzGurus);
            map.put("insert",true);
        } catch (Exception exception) {
            map.put("insert",false);
            exception.printStackTrace();
        }
        return map;
    }


}